package _examples;
import gui.simgui;
import javax.swing.JOptionPane; 
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Queue;
import java.util.Vector;

import jbotsim.Clock;
import jbotsim.Message;
import jbotsim.Messagebr;
import jbotsim.Node;
import jbotsim.Topology;
import jbotsim.event.ClockListener;
import jbotsim.event.MessageListener;
import jbotsim.event.MessagebrListener;
//RWPNode ���ƶ�����
public class PRouteNode extends RWPNode implements ClockListener, MessageListener  {
	//Ĭ���ʼ���Զ����һ�����νڵ㣿����Ŀ�ģ���
    
    
  
	public static  int macstatic=0;

	int lastFatherHello = 0-global.FATHERLOSTTIMER;
	int helloBefore=0;
	int requestBefore=0;
	long fatheraddr=0;
	int fatherid=0;
	int fathermac=0;
	long addr=1;
	int id=1;
	int mac=0;
	int level=0;
	int round=0;
	int prim=1;
	
	
	Vector<potFatherinfo> potfathervector=new Vector<potFatherinfo>();
	Vector<nodeinfo> childvec=new Vector<nodeinfo>();
	Vector<nodeinfo> nodevec=new Vector<nodeinfo>();
	int potfathermac=0;

	
	
	
	
	 
        
        
	 
	public boolean worked;
	boolean canmove;
	
	
	int pinground=0;
	int timebeg=0;
	int timeend=0;
	
	int pingsendnum=0;
	int pingrecvnum=0;
	int recvpinground=0;
	 
	boolean inping=false;
        
	
	//simgui mainframe;
	int sendjobid;
	int sendall;
	 
	int clientdesid;
	int senddatabeg;
	int reallysend=0;
	int sendthis=0;
	int lastsendstime;
	long clientdesaddr;
    int insend=0;
    int hangid;
    PRouteNode ttnode;
     boolean needqaddr;
    int qlevel;
    int prpktnum;
    int brpktnum;
    int maxjmp;
    String brpkt;
    String prpkt;
    boolean findbr;
    String clientaddrpath;
    int insendbr;
    int	sendthisbr;
    int lastsendstimebr;
        int         reallysendbr;
       int clientdesmac;
       int lastclocktime=0;
       
    public PRouteNode( ){
           
	
        super();
      //  JOptionPane.showInputDialog("proutenode" );
		 //System.out.print(macstatic);
  insendbr=0;
		mac=macstatic;
		macstatic++;
                
		if(mac==0)
		{
		 
			return ;
		}
             
		 Clock.addClockListener(this, global.TIMEPERCLOCK);
		  super.addMessageListener(this);
             
		 if(mac==1)
		 {
		  worked=true;
		  canmove=false;
		  level=1;	 
  
		  
		  this.setprmac(mac);
   	   this.setprid(id);
   	   this.setpraddr(addr);
 
		  
		  nodevec.add(new nodeinfo(round,mac,id,addr,1,1,Clock.currentTime()));
		  
		  
		  global.jv.addnode(this);
		 }
		 else
		 {
			 this.setprmac(mac);
		  worked=false;
		  canmove=false;
		  global.jv.addnode(this);
		 }
		 
	}
	
    
    
    
    
   
	public PRouteNode(simgui frame1){
           
		super();
		 //System.out.print(macstatic);
 mainframe=frame1;
		mac=macstatic;
		macstatic++;
		if(mac==0)
		{
			//���Է���ϵͳ�ʼ�Ὠ��һ��node������û����ʾ������ֱ�Ӻ�����һ������
			return ;
		}
                
                
		 Clock.addClockListener(this, global.TIMEPERCLOCK);
		 super.addMessageListener(this);
		 if(mac==1)
		 {
		  worked=true;
		  canmove=false;
		  level=1;	 
  
		  
		  this.setprmac(mac);
   	   this.setprid(id);
   	   this.setpraddr(addr);
 
		  
		  nodevec.add(new nodeinfo(round,mac,id,addr,1,1,Clock.currentTime()));
		  
		  
		  global.jv.addnode(this);
		 }
		 else
		 {
			 this.setprmac(mac);
		  worked=false;
		  canmove=false;
		  global.jv.addnode(this);
		 }
		 
	}
	
	
	
	
	//numpertime ��ʾһ��ʱ��Ƭ���͵İ���Ŀ��times��ʾ�ظ�������ٴ�
	//����ģ��ping��̣�pingÿ�η���numpertim������һֱ�ȴ�ܾó�ʱ���أ����lost�İٷֱȡ�
	//���һ��ظ�����times��������̡�
	public void rootsendping(long desip)
	{
		
		int times=0;
		int i=0;
	    long addr=0;
	    int j=0;
	    
	   
	  
	    timebeg=Clock.currentTime();
	    inping=true;
	    desip=192*256*256*256+168*256*256+1*256+desip;
	   
	    	for( i=0;i<nodevec.size();i++)
			{
				if(nodevec.get(i).ip==desip)
				{
					addr=nodevec.get(i).addr;
				    int senddes=finddes(addr);
				    pinground++;
				    System.out.print("send!!!!!!!!!!!!  \n\r");
				   double x=this.getcoords().x;
				   double y=this.getcoords().y;
					this.send(null, "PINGSEND srcaddr:"+id+" desaddr:"+addr+" round:"+pinground+" ptkid:" +times+" des:"+senddes+" x:"+x+" y:"+y+" total:"+0+" ");
					break;
				}
			}
			
	     	
	}
	
	public void clrchildarr()
	{
		childvec.clear();
	}
	
	public void onClock() {
		this.clrnuminclock();
                
             
	   if(id==1)
	   {
	   
	   }
	   else
	   {
		   super.onClock();
                   
	   }
		//if(id==1)
              //  {
                      
              //  }
	
                    if((Clock.currentTime()-lastsendstimebr)>(maxjmp*global.MAXWAITSENDS)&&insendbr==1)
                                    {           
                                        
                                        
                                     //   begqaddrbr(clientdesid);
                              
					
				}
		if(worked==true)
		{
                    
                    
                           
                    
                    
                  
					 
                              //          if(Clock.currentTime()-lastsendstimebr>(1*global.MAXWAITSENDS)&&insend==1)
				//{     
                                
                            
                            int find=0;
                            int time=Clock.currentTime();
                              int m=0;
                     while(true)          
                     {
                         find=0;
                     
                         for(m=0;m<childvec.size();m++)
                         {
                             if((time-childvec.get(m).time)>global.MAXCHILDLOST)
                             {
                                 childvec.remove(m);
                                 global.o.unlinkit(mac, childvec.get(m).mac);
                                 
                               //  global.o.nodes.get(childvec.get(m).mac).setProperty("color", "red");
                               //  int tt=0;
                               
                               //  ttnode.setProperty("color", "red");
                                 find=1;
                                 System.out.print(mac+" del child mac "+childvec.get(m).mac+"\n");
                                  break;
                             }
                         } 
                         if(find==0)
                             break;
                     }
                     String cmd="";
                       for(m=0;m<childvec.size();m++)
                         {
                            cmd=cmd+childvec.get(m).mac+",";
                         }
                     
			//worked well maybe
			helloBefore++;
			if(mac==1)
			{
				//root
				// System.out.print(helloBefore+"  ");
				if(helloBefore>global.HELLOTIMER)
				{
					//should send hello msg,root does not have father
					helloBefore=0;
					int kk=0;
					if(level==0)
					{
						kk++;
					}
                                        
					this.send(null, "HELLO mymac:"+mac+" myid:"+id+" myaddr:"+addr+" level:" +level+" remain:"+(global.MAXCHILD-childvec.size())+" cmd:"+cmd+" ");
				}
				
				
				//do ping!!!
				if(inping)
				{
					int timeint=Clock.currentTime() - timebeg;
					if ((global.MAXPINGWAIT< timeint))
					{
						//ping ��ʱ ֱ�� ����
 
						 
						   
		 
							JOptionPane.showMessageDialog(
					                  null,
					                  "��ʱ��",
					                  "ping ��Ϣ",
					                  JOptionPane.DEFAULT_OPTION );	 	
							inping=false;
					 
					 pingsendnum=pingrecvnum=0;
						
					}
				}
				
				
                                
                                if(Clock.currentTime()-lastsendstime>(global.QADDRMAX*global.MAXWAITSENDS)&&insend==1)
				{
					 
                                            if(mac==1)
                                            {
                                                
                                            }
                                            else
                                            {
                                                begqaddr(clientdesid);
                                            }
					
				}
                                
                                
                                 
                                      
                                      
                                
                                
				if(Clock.currentTime()-lastsendstime>global.MAXWAITSENDS&&insend==1&&clientdesaddr!=-1)
				{
					 
                                            sendnext();
					
				}
				 
			 
			}
			else
			{
				//not root
				if ((global.FATHERLOSTTIMER< Clock.currentTime() - lastFatherHello))
				{
					 
					if(super.ismarked()==1)	
					{
						
					}
					else
					{
						super.setProperty("color", "red");
					}
					//fatherlost , then this child tree should lost so do not send hello more! 
						
						worked=false;
						clrchildarr();
			        	   //this.setprid(0);
			        	   this.setpraddr(0);
			        	    
						//System.out.print("mac"+mac+"lost father mac"+fathermac+ "\n\r " ); 
						global.o.unlinkit(mac, fathermac);
						potfathervector.clear();
						round++;
						 if(potfathervector.size()==0)
							 return ;
						 
				}
				else
				{
					if(helloBefore>global.HELLOTIMER)
					{
						//should send hello msg
						helloBefore=0;
						int kk=0;
						if(level==0)
						{
							kk++;
						}
                                             
						this.send(null, "HELLO mymac:"+mac+" myid:"+id+" myaddr:"+addr+" level:" +level+" remain:"+(global.MAXCHILD-childvec.size())+" cmd:"+cmd+" ");
					}
                                        
                                        
                                          if(Clock.currentTime()-lastsendstime>(global.QADDRMAX*global.MAXWAITSENDS)&&insend==1)
				{
					 
                                            if(mac==1)
                                            {
                                                
                                            }
                                            else
                                            {
                                                begqaddr(clientdesid);
                                            }
					
				}
                                
                                          
                                          
                                     
                                
				if(Clock.currentTime()-lastsendstime>global.MAXWAITSENDS&&insend==1&&clientdesaddr!=-1)
				{
					 
                                            sendnext();
					
				}
                                
                                
                                
				}
			}
                        
				
		}
		else
		{
			//new join node
			requestBefore++;
			 if(potfathervector.size()==0)
				 return ;
			potFatherinfo potfather=bestFather();
			
			 if(requestBefore>global.REQUESTTIMER)
			 {
				 requestBefore=0;
			 this.send(null, "REQUEST fathermac:"+potfather.mac+" newnodemac:"+mac+" newnoderound:"+round+" desmac:"+potfather.mac+" srcmac:"+mac+" fatheraddr:"+potfather.addr+" " );
			 }
		}			
	}
	
	
	
	
	public potFatherinfo bestFather()
	{

		return  potfathervector.get(0);
	}
	
	public void doHello(Message msg)
	{
		//System.out.print("d0hello " +msg);
			String[] sArray=msg.toStringPR().split(":") ;
			String[] stmp1=sArray[1].split(" ");
			int potfmac=Integer.parseInt(stmp1[0]);
			
			String[] stmp2=sArray[2].split(" ");
			int potfid=Integer.parseInt(stmp2[0]);
			
			String[] stmp3=sArray[3].split(" ");
			long potfaddr=Long.parseLong(stmp3[0]);
			
			String[] stmp4=sArray[4].split(" ");
			int potflevel=Integer.parseInt(stmp4[0]);
                        
                        
                        String[] stmp5=sArray[5].split(" ");
			int remain=Integer.parseInt(stmp5[0]);
                        int childnum=global.MAXCHILD-remain;
                        
			//System.out.print(worked+","+id+","+fathermac+","+potfmac+","+fatherid+","+potfid+"\n\r");
			if(potfathervector.size()==0&&remain>0)
			{
			 
			 
			//	System.out.print("mac " +mac+"add level "+potflevel+" \r\n ");
				//System.out.print(msg.toStringPR()+"\r\n");
				potfathervector.add(0, new potFatherinfo(potfmac,potfid,Clock.currentTime(),potflevel,potfaddr));
			}
			else
			{
				//
			
				
				if( remain>0&&(potfathervector.get(0).level>potflevel||(Clock.currentTime()-potfathervector.get(0).time)>global.MAXPOTFATHERDROPTIME))
				{
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
				//5.8-------------------------修正了此bug，防止死锁！！！！加入了超时判断，同时必须更新round！！
                                    round++;
                                    //	System.out.print("mac " +mac+"changed level "+potfathervector.get(0).level+"to "+potflevel +" \r\n ");
					potfathervector.set(0, new potFatherinfo(potfmac,potfid,Clock.currentTime(),potflevel,potfaddr));
				}
			}
			
			if(worked==true)
			{
				// System.out.print(worked+","+id+","+fathermac+","+potfmac+","+fatherid+","+potfid+"\n\r");
				//String numstr=msg.subString; 
                            
                            int m=0;
                               for(m=0;m<childvec.size();m++)
                               {
                                   if(childvec.get(m).mac==potfmac)
                                   {
                                       childvec.get(m).time=Clock.currentTime();
                                   }
                               }
                               
                               
                               
                               
				if(id==1)
				{
				  
				}
				else
				{
					
					if(fathermac==potfmac&&fatheraddr==potfaddr)
					{
						//System.out.print(mac+" updata father !!!\r\n");
						 String[] stmp6=sArray[6].split(" ");
                                                
                                                 if(childnum!=0)
                                                 {
                                                      String[] macarr=stmp6[0].split(",");
                                               for(m=0;m<childnum;m++)
                                               {
                                                    if(Integer.parseInt(macarr[m])==mac)
                                                    {
                                                        lastFatherHello=Clock.currentTime();
                                                    }
                                               }
                                                 }
                                                
			                          
						
					}
				}
			}
			else
			{
			
				 
			}

	}
	
	public boolean isprim(int n)
	{
	 if (2 == n)
	  return true;
	 int  i;
	 for (i = 2; (i*i) <= n; ++i) {
	  if (0 == n % i)
	   return false;
	      }
	 return true;
	}

	public int allocPrim()
	{
		int i;
	    for( i=prim+1;i<global.MAXPRIM;i++)
	    	{
	    	   if(isprim(i))
	    	   	{
	    	   	prim=i;
	    	   	  return i;
	    	   	}

	    	}
		if(i==global.MAXPRIM)
			{
			 System.out.print("out of prim \n");
			return 0;
			}
		return 0;

	}
	
	
	public int finddes(long addr)
	{
		int i;
		for(i=0;i<childvec.size();i++)
		{
			if(addr%childvec.get(i).id==0)
			{
				return childvec.get(i).mac;
			}
		}
		 System.out.print("not find child!\r\n");
		return -1;
	}
	
	
	public void doRequest(Message msg){
		
		//this.send(null, "REQUEST fathermac:"+potfathermac+" newnodemac:"+mac+" newnoderound:"+round+" desmac:"+father+" srcmac:"+mac);
		String[] sArray=msg.toStringPR().split(":") ;
		String[] stmp1=sArray[1].split(" ");
		int tfathermac=Integer.parseInt(stmp1[0]);
		String[] stmp2=sArray[2].split(" ");
		int tchildmac=Integer.parseInt(stmp2[0]);
		String[] stmp3=sArray[3].split(" ");
		int tchildround=Integer.parseInt(stmp3[0]);
		String[] stmp4=sArray[4].split(" ");
		int tdesmac=Integer.parseInt(stmp4[0]);
		String[] stmp5=sArray[5].split(" ");
		int tsrcmac=Integer.parseInt(stmp5[0]);
		String[] stmp6=sArray[6].split(" ");
		long tfatheraddr=Long.parseLong(stmp6[0]);
		 //System.out.print(tdesmac+"+"+mac+"   ");
        if(tdesmac!=mac)
         return;
		
	
     //  System.out.print(msg.toStringPR()+"\r\n");
        if(mac==1)
        {
        	
        	//root
        	 long addrf=-1;
        	 int idf=-1;
    		 for(int k=0;k<nodevec.size();k++)
    		 {
    			 if(nodevec.get(k).mac==tfathermac)
    			 {
    				 addrf=nodevec.get(k).addr;
    				 idf=nodevec.get(k).id;
    				 break;
    			 }
    		 }
    		 if(addrf==-1)
    		 {
    			 int abc=22;
    		 }
    	 
        	//root ��������
        	// System.out.print("root recv  " +msg);
        	int i=0;
        	if(tfathermac==mac)
        	{
        		//root ��child
        		
        		
        	//	 System.out.print("root's child ");
        		//System.out.print(msg.toStringPR()+"\r\n");
           		for(i=0;i<nodevec.size();i++)
        		{
            		if(nodevec.get(i).mac==tchildmac)
            		{
            			//�ظ��ģ�����
            			if(nodevec.get(i).round<tchildround)
            			{
            				//  System.out.print("root recv  update   "+tchildmac+"   "+tchildround+"     \r\n");
            				//ע�⼴ʹ��ͬҲҪ���ͣ������⣬father�Ѿ�addchild��newnode����ʱ��ʧ��newnode��ͬһround����request������������
            				//��newnodeһֱ����֪�������յ�prim
            				//new round!!!! new node ask for new father
            				 int primtmp=nodevec.get(i).id;
            				int stat= addchild(tchildmac,tchildround,primtmp);
            				
                                        if(stat==0)
                                        {
                                            //not add!!!
                                            return;
                                        }
            				 this.send(null, "ROOTGIVEPRIM fathermac:"+tfathermac+" newnodemac:"+tchildmac+" newnoderound:"+tchildround+" desmac:"+tsrcmac+" srcmac:"+mac+" prim:"+primtmp+" fatheraddr:"+1+" level:"+level+" ");;
            				 nodevec.get(i).round=tchildround;
            				 if(nodevec.get(i).fathermac!=tfathermac)
            				 {
            					 //tell old father lost!!!!
            					int desmac1= finddes(nodevec.get(i).addr/nodevec.get(i).id);
            					//System.out.print("ROOTTELLLOST fathermac:"+nodevec.get(i).fathermac+" lostchildmac:"+nodevec.get(i).mac+" nodecurrentround:"+tchildround+" desmac:"+desmac1+" srcmac:"+mac+" fatheraddr:"+nodevec.get(i).addr+" "+"\r\n");
            					 this.send(null, "ROOTTELLLOST fathermac:"+nodevec.get(i).fathermac+" lostchildmac:"+nodevec.get(i).mac+" nodecurrentround:"+tchildround+" desmac:"+desmac1+" srcmac:"+mac+" fatheraddr:"+nodevec.get(i).addr+" ");
            				    nodevec.get(i).addr=1*primtmp;
            				    nodevec.get(i).fatherid=1;
            				    nodevec.get(i).fathermac=1;
            				    nodevec.get(i).id=primtmp;
            				    nodevec.get(i).round=tchildround;
            				 }
            				 
            			}
            			else if(nodevec.get(i).round==tchildround)
            			{
            				//  System.out.print("root recv  update   "+tchildmac+"   "+tchildround+"     \r\n");
            				//ע�⼴ʹ��ͬҲҪ���ͣ������⣬father�Ѿ�addchild��newnode����ʱ��ʧ��newnode��ͬһround����request������������
            				//��newnodeһֱ����֪�������յ�prim
            				//new round!!!! new node ask for new father
            				 
            				
            				 this.send(null, "ROOTGIVEPRIM fathermac:"+tfathermac+" newnodemac:"+tchildmac+" newnoderound:"+tchildround+" desmac:"+tsrcmac+" srcmac:"+mac+" prim:"+nodevec.get(i).id+" fatheraddr:"+1+" level:"+level+" ");
            			 
            				 
            			}
            			else
            			{
            				//old������
            				
            			}
            			return;
            		}
            	}
           		 // System.out.print("root recv  addchild  "+tchildmac+" round:     "+tchildround+"     \r\n");
        		  int primtmp=allocPrim();
           		nodevec.add(new nodeinfo(tchildround,tchildmac,primtmp,primtmp,1,1,Clock.currentTime()));
                
        		int stat=addchild(tchildmac,tchildround,primtmp);
                        if(stat==0)
                            return;
        //		 System.out.print("root recv  addchil  mac "+tchildmac+"   ");
   			    this.send(null, "ROOTGIVEPRIM fathermac:"+tfathermac+" newnodemac:"+tchildmac+" newnoderound:"+tchildround+" desmac:"+tsrcmac+" srcmac:"+mac+" prim:"+primtmp+" fatheraddr:"+1+" level:"+level+" ");
   			    
        	}
        	else
        	{
        		//��root��child
        		for(i=0;i<nodevec.size();i++)
        		{
            		if(nodevec.get(i).mac==tchildmac)
            		{
            			//�ظ��ģ�����
            			if(nodevec.get(i).round<tchildround)
            			{
            				//ע�⼴ʹ��ͬҲҪ���ͣ������⣬father�Ѿ�addchild��newnode����ʱ��ʧ��newnode��ͬһround����request������������
            				//��newnodeһֱ����֪�������յ�prim
            				//new round!!!! new node ask for new father
            				 int primtmp=nodevec.get(i).id;
                                         nodevec.get(i).addr=primtmp*addrf;
            				 this.send(null, "ROOTGIVEPRIM fathermac:"+tfathermac+" newnodemac:"+tchildmac+" newnoderound:"+tchildround+" desmac:"+tsrcmac+" srcmac:"+mac+" prim:"+primtmp+" fatheraddr:"+addrf+" level:"+level+" ");
            				 nodevec.get(i).round=tchildround;
            				 if(nodevec.get(i).fathermac!=tfathermac)
            				 {
            					 //tell old father lost!!!!
            					int desmac1= finddes(nodevec.get(i).addr/nodevec.get(i).id);
            					//System.out.print("ROOTTELLLOST fathermac:"+nodevec.get(i).fathermac+" lostchildmac:"+nodevec.get(i).mac+" nodecurrentround:"+tchildround+" desmac:"+desmac1+" srcmac:"+mac+" fatheraddr:"+nodevec.get(i).addr/nodevec.get(i).id+" "+"\r\n");
            					 this.send(null, "ROOTTELLLOST fathermac:"+nodevec.get(i).fathermac+" lostchildmac:"+nodevec.get(i).mac+" nodecurrentround:"+tchildround+" desmac:"+desmac1+" srcmac:"+mac+" fatheraddr:"+nodevec.get(i).addr/nodevec.get(i).id+" ");
            					    nodevec.get(i).addr=addrf*primtmp;
                				    nodevec.get(i).fatherid=idf;
                				    nodevec.get(i).fathermac=tfathermac;
                				    nodevec.get(i).id=primtmp;
                				    nodevec.get(i).round=tchildround;
            				 }
            				 
            			}
            			else if(nodevec.get(i).round==tchildround)
            			{
            				 this.send(null, "ROOTGIVEPRIM fathermac:"+tfathermac+" newnodemac:"+tchildmac+" newnoderound:"+tchildround+" desmac:"+tsrcmac+" srcmac:"+mac+" prim:"+nodevec.get(i).id+" fatheraddr:"+addrf+" level:"+level+" ");
            				 
   
            			}
            			else
            			{
            				
            			}
            			return;
            		}
            	}
        		//���ظ��ģ�������root��child
        
       		int primtmp=allocPrim();
        		nodevec.add(new nodeinfo(tchildround,tchildmac,primtmp,primtmp*addrf,tfathermac,idf,Clock.currentTime()));
           	  
   			    this.send(null, "ROOTGIVEPRIM fathermac:"+tfathermac+" newnodemac:"+tchildmac+" newnoderound:"+tchildround+" desmac:"+tsrcmac+" srcmac:"+mac+" prim:"+primtmp+" fatheraddr:"+addrf+" level:"+level+" ");
   			     return;
        	}
        	 
        	 
        	 
        }
        else
        {
        	//not root,so  send to father
        	this.send(null, "REQUEST fathermac:"+tfathermac+" newnodemac:"+tchildmac+" newnoderound:"+tchildround+" desmac:"+fathermac+" srcmac:"+mac+" fatheraddr:"+tfatheraddr+" " );
       // this.send(null, "REQUEST fathermac:"+fathermac+" newnodemac:"+childmac+" newnoderound:"+childround+" desmac:"+fathermac+" ");
        }
		 
		
	}
 
	public void doRootgiveprim(Message msg){
	//	ROOTGIVEPRIM fathermac:"+fathermac+" newnodemac:"+childmac+" newnoderound:"+childround+" desmac:"+srcmac+" srcmac:"+mac+" prim:"+primtmp+" fatheraddr:"+fatheraddr+" ");
	//	 System.out.print(msg.toStringPR());
		String[] sArray=msg.toStringPR().split(":") ;
		String[] stmp1=sArray[1].split(" ");
		int tfathermac=Integer.parseInt(stmp1[0]);
		String[] stmp2=sArray[2].split(" ");
		int tchildmac=Integer.parseInt(stmp2[0]);
		String[] stmp3=sArray[3].split(" ");
		int tchildround=Integer.parseInt(stmp3[0]);
		String[] stmp4=sArray[4].split(" ");
		int tdesmac=Integer.parseInt(stmp4[0]);
		String[] stmp5=sArray[5].split(" ");
		int tsrcmac=Integer.parseInt(stmp5[0]);
		String[] stmp6=sArray[6].split(" ");
		int tprimthis=Integer.parseInt(stmp6[0]);
		// System.out.print(sArray[6]+"             ");
		String[] stmp7=sArray[7].split(" ");
		long tfatheraddr=Long.parseLong(stmp7[0]);
		
		if(tdesmac!=mac)
		{	
			return;
		}
		 
       if(tfathermac==mac)
		{
    	   // this is father
			int stat=addchild(tchildmac,tchildround,tprimthis);
                        if(stat==0)
                            return;
			//System.out.print("mac "+mac+"addchild mac "+tchildmac+"tchildround : "+tchildround+"tprimthis: "+tprimthis+"\\n\r");
			this.send(null, "ROOTGIVEPRIM fathermac:"+tfathermac+" newnodemac:"+tchildmac+" newnoderound:"+tchildround+" desmac:"+tchildmac+" srcmac:"+mac+" prim:"+tprimthis+" fatheraddr:"+tfatheraddr+" level:"+level+" ");
		}
       else if(mac==tchildmac)
       {
    	  //this is child
    	   //��bug���������father������addchild������newnode���ͣ�����newnodeû���յ�����ʱ
    	   //father���Ӧchild��round�Ѿ���󣬵���newnode��֪����������ͬһ��roundrequest����ʱ
    	   //root��father�����ԣ���������ʱnode�����������������node�ж�potfaher����Ҫ�����������ʹ�ã���
    	   //����ĳ���ڵ�һֱû��Ӧ��һ����
    	  // System.out.print("node recv!!!!       ");
    	   if(worked==true)
    		   return;
    	   if(tchildround<round)
    	   {
    		   
    	   }
    	   else
    	   {
    		   //node join
    		   
        	   fathermac=tfathermac;
        	   fatheraddr=tfatheraddr;
        	   id=tprimthis;
        	   String[] stmp8=sArray[8].split(" ");
       	  level=Integer.parseInt(stmp8[0])+1;
        	 
             addr=id*fatheraddr;
         
        	   worked=true;
        	   
        	   
        	   lastFatherHello=Clock.currentTime();
				if(super.ismarked()==1)	
				{
					
				}
				else
				{
					 super.setProperty("color", "green");
				}
        	  
        	   this.setprid(id);
        	   this.setpraddr(addr);
        	   this.setprmac(mac);
        	//    System.out.print("node mac "+mac+"id "+tprimthis+" fathermac:"+fathermac+" round:"+round+ "level : "+level+" join!! \\n\r");
        	    global.o.linkit(mac,fathermac);
        	    round++;
    	   }

    	   
       }
		else
		{
			//send down
			int desmac1= finddes(tfatheraddr);
			this.send(null, "ROOTGIVEPRIM fathermac:"+tfathermac+" newnodemac:"+tchildmac+" newnoderound:"+tchildround+" desmac:"+desmac1+" srcmac:"+mac+" prim:"+tprimthis+" fatheraddr:"+tfatheraddr+" level:"+level+" ");
		}
		
	}
	
	public void delchild(int childmac, int round)
	{
		int i;
		for(i=0;i<childvec.size();i++)
		{
			if(childvec.get(i).mac==childmac)
			{
				if(childvec.get(i).round>round)
				{
					
				}
				else
				{
					//roung��ȣ���ҲҪ����
					childvec.set(i, childvec.get(childvec.size()-1));
					childvec.remove((childvec.size()-1));
	
				}
				return;
			}
			
		}
		System.out.print("delchild err\r\n");
	}
	public void doroottelllost(Message msg)
	{
		 //fathermac:"+nodevec.get(i).fathermac+" lostchildmac:"+nodevec.get(i).mac+" nodecurrentround:"+tchildround+" desmac:"+desmac1+" srcmac:"+mac+" fatheraddr:"+nodevec.get(i).addr/nodevec.get(i).id+" ");
			 String[] sArray=msg.toStringPR().split(":") ;
			String[] stmp1=sArray[1].split(" ");
			int tfathermac=Integer.parseInt(stmp1[0]);
			String[] stmp2=sArray[2].split(" ");
			int tlostchildmac=Integer.parseInt(stmp2[0]);
			String[] stmp3=sArray[3].split(" ");
			int tchildround=Integer.parseInt(stmp3[0]);
			String[] stmp4=sArray[4].split(" ");
			int tdesmac=Integer.parseInt(stmp4[0]);
			String[] stmp5=sArray[5].split(" ");
			int tsrcmac=Integer.parseInt(stmp5[0]);
			String[] stmp6=sArray[6].split(" ");
			long tfatheraddr=Long.parseLong(stmp6[0]);
		 
			
			if(tdesmac!=mac)
			{	
				return;
			}
			 
	       if(tfathermac==mac)
			{
	    	   // this is father
				delchild(tlostchildmac,tchildround);
				System.out.print("mac "+mac+" del childmac "+tlostchildmac+"\r\n");
			//	System.out.print("mac "+mac+"addchild mac "+tchildmac+"tchildround : "+tchildround+"tprimthis: "+tprimthis+"\\n\r");
				//this.send(null, "ROOTGIVEPRIM fathermac:"+tfathermac+" newnodemac:"+tchildmac+" newnoderound:"+tchildround+" desmac:"+tchildmac+" srcmac:"+mac+" prim:"+tprimthis+" fatheraddr:"+tfatheraddr+" level:"+level+" ");
			}

			else
			{
				//send down
				int desmac1= finddes(tfatheraddr);
				System.out.print("send   mac "+mac+" del childmac "+tlostchildmac+"\r\n");
				 this.send(null, "ROOTTELLLOST fathermac:"+tfathermac+" lostchildmac:"+tlostchildmac+" nodecurrentround:"+tchildround+" desmac:"+desmac1+" srcmac:"+mac+" fatheraddr:"+tfatheraddr+" ");
			 
			}
	}
	public int addchild(int childmac, int round,int prim)
	{
		int i;
                
		for(i=0;i<childvec.size();i++)
		{
			if(childvec.get(i).mac==childmac)
			{
				if(childvec.get(i).round>round)
				{
					
				}
				else
				{
					//roung��ȣ���ҲҪ����
					childvec.get(i).id=prim;
					childvec.get(i).round=round;
					childvec.get(i).addr=addr*prim;
					
				}
				return 1;
			}
			
		}
 
		if(childvec.size()>=global.MAXCHILD)
                    return 0;
          
		childvec.add(new nodeinfo(round,childmac,prim,prim*addr,mac,id,Clock.currentTime()));
		return 1;
	}
	
	
	public double getweight(double x, double y, double xt, double yt)
	{
		
		 
		return global.LENGTHWEIGHT*Math.sqrt(Math.pow((x-xt), 2)+Math.pow((y-yt), 2))+global.COMPUTETIMEWEIGHT;
	}
	public void doping(Message msg)
	{
		//System.out.print(msg.toStringPR());
		//this.send(null, "PINGSEND srcaddr:"+id+" desaddr:"+addr+" round:"+pinground+" ptkid:" +times+" ");
		 String[] sArray=msg.toStringPR().split(":") ;
			String[] stmp1=sArray[1].split(" ");
			long srcaddr=Long.parseLong(stmp1[0]);
			String[] stmp2=sArray[2].split(" ");
			long desaddr=Long.parseLong(stmp2[0]);
			String[] stmp3=sArray[3].split(" ");
			int pinground=Integer.parseInt(stmp3[0]);
			String[] stmp4=sArray[4].split(" ");
			int times=Integer.parseInt(stmp4[0]);
			String[] stmp5=sArray[5].split(" ");
			int senddes=Integer.parseInt(stmp5[0]);
			String[] stmp6=sArray[6].split(" ");
			double xt=Double.parseDouble(stmp6[0]);
			String[] stmp7=sArray[7].split(" ");
			double yt=Double.parseDouble(stmp7[0]);
			String[] stmp8=sArray[8].split(" ");
			double tot=Double.parseDouble(stmp8[0]);
			
			//System.out.print("mac "+mac+"doping \r\n");
			if(mac!=senddes)
			{
				return;
			}
			//System.out.print(msg.toStringPR());
			if(desaddr==addr)
			{
				
				if(recvpinground>=pinground)
				{
					//old ping packet!! has responded before
					return ;
				}
			//	System.out.print("mac "+mac+"recv ping!!\n\r");
				
				   double x=this.getcoords().x;
				   double y=this.getcoords().y;
				   
				   tot=tot+getweight(x,y,xt,yt);
					//this.send(null, "PINGSEND srcaddr:"+id+" desaddr:"+addr+" round:"+pinground+" ptkid:" +times+" des:"+senddes+" x:"+x+" y:"+y+" total:"+" ");
					
					
				this.send(null, "PINGREPLAY srcaddr:"+desaddr+" desaddr:"+srcaddr+" round:"+pinground+" ptkid:" +times+" des:"+fathermac+" x:"+x+" y:"+y+" total:"+tot+" ");
				recvpinground++;
				
			}
			else
			{
				 double x=this.getcoords().x;
				   double y=this.getcoords().y;
				   
				   tot=tot+getweight(x,y,xt,yt);
				senddes=finddes(desaddr);
				this.send(null, "PINGSEND srcaddr:"+srcaddr+" desaddr:"+desaddr+" round:"+pinground+" ptkid:" +times+" des:"+senddes+" x:"+x+" y:"+y+" total:"+tot+" ");
			}
	}
	
	
	public void dopingreply(Message msg)
	{
		//this.send(null, "PINGSEND srcaddr:"+id+" desaddr:"+addr+" round:"+pinground+" ptkid:" +times+" ");
		 String[] sArray=msg.toStringPR().split(":") ;
			String[] stmp1=sArray[1].split(" ");
			long srcaddr=Long.parseLong(stmp1[0]);
			String[] stmp2=sArray[2].split(" ");
			long desaddr=Long.parseLong(stmp2[0]);
			String[] stmp3=sArray[3].split(" ");
			int pinground=Integer.parseInt(stmp3[0]);
			String[] stmp4=sArray[4].split(" ");
			int times=Integer.parseInt(stmp4[0]);
			String[] stmp5=sArray[5].split(" ");
			int senddes=Integer.parseInt(stmp5[0]);
			String[] stmp6=sArray[6].split(" ");
			double xt=Double.parseDouble(stmp6[0]);
			String[] stmp7=sArray[7].split(" ");
			double yt=Double.parseDouble(stmp7[0]);
			String[] stmp8=sArray[8].split(" ");
			double tot=Double.parseDouble(stmp8[0]);
			if(mac!=senddes)
			{
				return;
			}
			//System.out.print("recved     "+msg.toStringPR());
			if(desaddr==addr)
			{
				
				if(recvpinground>=pinground)
				{
					//old ping packet!! has responded before
					return ;
				}
				
				
				
				if(addr==1)
				{
					 
					 
						//timeend=Clock.currentTime();
						int timeint=timeend-timebeg;
						
						 double x=this.getcoords().x;
						   double y=this.getcoords().y;
						   
						   tot=tot+getweight(x,y,xt,yt);
						//senddes=finddes(desaddr);
						//this.send(null, "PINGSEND srcaddr:"+srcaddr+" desaddr:"+desaddr+" round:"+pinground+" ptkid:" +times+" des:"+senddes+" x:"+x+" y:"+y+" total:"+tot+" ");
						
						
						JOptionPane.showMessageDialog(
				                  null,
				                  "����ʱ�䵥λ��"+(long)tot,
				                  "ping ��Ϣ",
				                  JOptionPane.DEFAULT_OPTION );	 	
						inping=false;
				 
				}
				else
				{
					
				}
				 
				
			}
			else
			{
				// System.out.print("upto!!\r\n");
				 double x=this.getcoords().x;
				   double y=this.getcoords().y;
				   
				   tot=tot+getweight(x,y,xt,yt);
				//senddes=finddes(desaddr);
				 this.send(null, "PINGREPLAY srcaddr:"+srcaddr+" desaddr:"+desaddr+" round:"+pinground+" ptkid:" +times+" des:"+fathermac+" x:"+x+" y:"+y+" total:"+tot+" ");
				 System.out.print("send     PINGREPLAY srcaddr:"+srcaddr+" desaddr:"+desaddr+" round:"+pinground+" ptkid:" +times+" des:"+fathermac+" x:"+x+" y:"+y+" total:"+tot+" ");
			}
	}
	
	public void dodata(Message msg)
	{
		//this.send(null, "DATA  srcaddr:"+id+" desaddr:"+addr+" len:"+323+" ptkid:" +pktid+" "+" desmac:"+ desmac+" ");
		 String[] sArray=msg.toStringPR().split(":") ;
			String[] stmp1=sArray[1].split(" ");
			long srcaddr=Long.parseLong(stmp1[0]);
			String[] stmp2=sArray[2].split(" ");
			long desaddr=Long.parseLong(stmp2[0]);
		 
			String[] stmp3=sArray[3].split(" ");
			int desid=Integer.parseInt(stmp3[0]);
			String[] stmp4=sArray[4].split(" ");
			int pktid=Integer.parseInt(stmp4[0]);
			String[] stmp5=sArray[5].split(" ");
			int desmac=Integer.parseInt(stmp5[0]);
			String[] stmp6=sArray[6].split(" ");
			int srcid=Integer.parseInt(stmp6[0]);
			if(desmac!=mac)
				return;
			if(addr!=desaddr)
			{
                            if(desaddr%addr==0)
                            {
                               desmac=finddes(desaddr);
                            }
                            else
                            {
                                desmac=fathermac;
                            }
				 
				this.send(null, "DATA  srcaddr:"+srcaddr+" desaddr:"+desaddr+" desid:"+desid+" ptkid:" +pktid+" "+" desmac:"+ desmac+" srcid:"+srcid+" ");
			}
			else
			{
				if(desid!=id)
				{
					System.out.print("bug desaddr match but id not match");
					return;
				}
				if(pktid!=0)
				{
					
				}
				else
				{
			//		JOptionPane.showInputDialog("Ŀ�ĵ�ַ�յ����η������а���");
                                    if(id==1)
                                    {
                                        this.send(null, "DATARECV  srcaddr:"+desaddr+" desaddr:"+srcaddr+" desid:"+srcid+" ptkid:" +pktid+" "+" desmac:"+ finddes(srcaddr)+" srcid:"+desid+" ");
                                    }
                                    else
                                    {
                                        this.send(null, "DATARECV  srcaddr:"+desaddr+" desaddr:"+srcaddr+" desid:"+srcid+" ptkid:" +pktid+" "+" desmac:"+ fathermac+" srcid:"+desid+" ");
                                    }
					
				}
			}
			
			 
			
	}
	
        
        
          public void begqaddrbr(int qid)
        {
            //  JOptionPane.showInputDialog("beg q addr");
              clientdesaddr=-1;
              global.brfind=false;
          int lv=0;
          maxjmp++;
         
		    this.sendbr(null,"BRQES srcid:"+mac+" desid:"+clientdesmac+" path:"+mac+", lv:"+lv+" max:"+maxjmp+" ");
           
        }
        
        public void begqaddr(int qid)
        {
            //  JOptionPane.showInputDialog("beg q addr");
              clientdesaddr=-1;
              global.brfind=false;
          int lv=0;
		   this.send(null,"BRQEST srcid:"+id+" desid:"+clientdesid+" path:"+id+", lv:"+lv+" ");
             this.send(null, "QDEST  srcaddr:"+addr+" desaddr:"+1+" desid:"+1+" desmac:"+ fathermac+" srcid:"+id+" qid:"+qid+" qaddr:"+0+" level:"+qlevel+" ");
        }
        
    
	public void begsend(int desid,int datalen,PRouteNode t,int i,int desmac)
	{
            brpkt="";
            prpkt="";
            prpktnum=0;
            brpktnum=0;
            maxjmp=5;
           
            clientdesmac=desmac;
            clientaddrpath="";
            if(insend==1||insendbr==1)
            { 
                JOptionPane.showInputDialog("节点id "+id+" 正在向节点id "+clientdesid+" 发送数据，请求被忽略");
                return;
            }
            insendbr=1;
               lastsendstimebr=Clock.currentTime();
             senddatabeg=Clock.currentTime();
            this.setProperty("color", "ORANGE");
            if(mac==1)
            {
                 //  mainframe=mainframe1;
            sendjobid=i;
		//  app=new jprocessbar(desid);
		  insend=1;
		
		sendall=datalen;
		reallysend=0;
		
		clientdesid=desid;
		clientdesaddr=finddesaddrfromid(clientdesid);
               
	ttnode=t;
	ttnode.setProperty("color", "ORANGE");
	ttnode.marksend();
	//	JOptionPane.showInputDialog("׼����id"+desid+" ��ַ"+clientdesaddr+"���������"+datalen);
           JOptionPane.showInputDialog("节点id "+id+" 准备向节点id "+clientdesid+" 发送数据");
		sendnext();
           //     sendnextbr();
            }
            else
            {
                qlevel=1;
                needqaddr=true;
              //     mainframe=mainframe1;
            sendjobid=i;
		//  app=new jprocessbar(desid);
		  insend=1;
                  sendall=datalen;
		reallysend=0;
		clientdesaddr=-1;
		clientdesid=desid;
                ttnode=t;
	ttnode.setProperty("color", "ORANGE");
	ttnode.marksend();
          lastsendstime=Clock.currentTime();
       JOptionPane.showInputDialog("节点id "+id+" 准备向节点id "+clientdesid+" 发送数据");
          sendnext();
      //    sendnextbr();
      //  begqaddr(desid);
    //    begqaddrbr(desid);
       // sendnext();
            }
           
	}
	
	public long finddesaddrfromid(int clientdesid)
	{
		return rootfinddesaddrfromid(clientdesid);
	}
	
	
	public long rootfinddesaddrfromid(int clientdesid)
	{
		
		int i;
		for(i=0;i<nodevec.size();i++)
		{
    		if(nodevec.get(i).id==clientdesid)
    		{
    			long tmp=nodevec.get(i).addr;
    			return tmp;
    		}
		}
		return 0;
	}
        
        
	public void sendnext()
	{
           
              if(mac==1)
              {
                   clientdesaddr=finddesaddrfromid(clientdesid);
              }
              else
              {
                  if(clientdesaddr==-1)
                      return;
              }
             lastsendstime=Clock.currentTime();
		//senddatabeg=Clock.currentTime();
		 
		sendthis=0;
		int i=0;
		
		if(clientdesaddr==0)
		{
			System.out.print("send data but not find addr according to id!! bug return");
			return;
		}
         //       JOptionPane.showInputDialog("ready send");
		 int sendnum=0;
		 int pktid;
		 int desmac;
                 if(addr%clientdesaddr==0)
                 {
                     desmac=finddes(clientdesaddr);
                 }
		 else
                 {
                     desmac=fathermac;
                 }
		 for(i=0;i<global.TIMEPERSENDS;i++)
		 {
			 
			 
			 if((reallysend+sendthis)>=sendall)
			 {
				// sendnum=sendremain+MAXPERSEND;
				  sendthis=sendall-reallysend;
				 pktid=0;
                             
					this.send(null, "DATA  srcaddr:"+addr+" desaddr:"+clientdesaddr+" desid:"+clientdesid+" ptkid:" +pktid+" "+" desmac:"+ desmac+" srcid:"+id+" ");
					return;
			 }
			 else
			 {
				// sendnum=MAXPERSEND;
				 sendthis+=global.MAXPERSEND;
				 pktid=1;
                                 
				 this.send(null, "DATA  srcaddr:"+addr+" desaddr:"+clientdesaddr+" desid:"+clientdesid+" ptkid:" +pktid+" "+" desmac:"+ desmac+" srcid:"+id+" ");
			 }
		 }
		 pktid=0;
		 
			this.send(null, "DATA  srcaddr:"+addr+" desaddr:"+clientdesaddr+" desid:"+clientdesid+" ptkid:" +pktid+" "+" desmac:"+ desmac+" srcid:"+id+" ");
	}
        
        
        
        public void sendnextbr()
	{
           /*
              if(mac==1)
              {
                   clientdesaddr=finddesaddrfromid(clientdesid);
              }
              else
              {
                  if(clientdesaddr==-1)
                      return;
              }*/
            if(clientaddrpath=="")
                return;
             lastsendstimebr=Clock.currentTime();
		//senddatabeg=Clock.currentTime();
		 
		sendthisbr=0;
		int i=0;

         //       JOptionPane.showInputDialog("ready send");
		 int sendnum=0;
		 int pktid;
		 int desmac;
             
                 
                 int nextmac;
                  	 String[] sArray=clientaddrpath.split(",") ;
		 
			nextmac=Integer.parseInt(sArray[1]);
		 for(i=0;i<global.TIMEPERSENDS;i++)
		 {
			 
			 
			 if((reallysendbr+sendthisbr)>=sendall)
			 {
				// sendnum=sendremain+MAXPERSEND;
				  sendthis=sendall-reallysendbr;
				 pktid=0;
                             
					this.sendbr(null, "DTBRS  srcid:"+mac+" desid:"+clientdesmac+" path:"+clientaddrpath+" ptkid:" +pktid+" nextmac:"+ nextmac+" ");
					return;
			 }
			 else
			 {
				// sendnum=MAXPERSEND;
				 sendthisbr+=global.MAXPERSEND;
				 pktid=1;
                                 
				this.sendbr(null, "DTBRS  srcid:"+mac+" desid:"+clientdesmac+" path:"+clientaddrpath+" ptkid:" +pktid+" nextmac:"+ nextmac+" ");
			 }
		 }
		 pktid=0;
		 
		this.sendbr(null, "DTBRS  srcid:"+mac+" desid:"+clientdesmac+" path:"+clientaddrpath+" ptkid:" +pktid+" nextmac:"+ nextmac+" ");
	}
        
        
        
	public void recvup(int id)
	{
		
		if(clientdesid==id)
		{
			 //JOptionPane.showInputDialog("recvup  "+sendthis+"  and reallysend "+(reallysend+sendthis));
			if((reallysend+sendthis)>=sendall)
			{
				 mainframe.processbarchange(sendjobid, 100);
				insend=0;
                                 this.setProperty("color", "green");
                                 this.unmarksend();
                                 if(ttnode.worked==true)
                                 {
                                     ttnode.setProperty("color", "green");
                                 }
                                 else
                                 {
                                     JOptionPane.showInputDialog("prime route send all data over!!! use time");
                                     ttnode.setProperty("color", "red");
                                 }
				
                             //   mainframe.addtext(1,prpkt);
                              //  mainframe.addtext(2,brpkt);
				ttnode.unmarksend();
				JOptionPane.showInputDialog("prime route send all data over!!! use time"+(Clock.currentTime()-senddatabeg));
                                 mainframe.processbarchange(sendjobid, 0);
				//System.out.print("send all data over!!! use time"+(Clock.currentTime()-senddatabeg));
			}
			else
			{
				reallysend+=sendthis;
				int mm=reallysend*100/sendall;
                             // JOptionPane.showInputDialog("recv up" );
                                mainframe.processbarchange(sendjobid, mm);
			//	JOptionPane.showInputDialog(mm+" "+reallysend+"  "+sendall);
			      
				sendnext();
			}
			
		}
	}
        
        
        
        
        public void recvupbr( )
	{
		
		 if(insendbr==0)
                     return;
			 //JOptionPane.showInputDialog("recvup  "+sendthis+"  and reallysend "+(reallysend+sendthis));
			if((reallysendbr+sendthisbr)>=sendall)
			{
				mainframe.processbarchange(sendjobid+4, 100);
				insendbr=0;
                              //   this.setProperty("color", "green");
                              //   this.unmarksend();
                               
				
                            //    mainframe.addtext(1,prpkt);
                             //   mainframe.addtext(2,brpkt);
			//	ttnode.unmarksend();
				JOptionPane.showInputDialog("br send all data over!!! use time"+(Clock.currentTime()-senddatabeg));
                               mainframe.processbarchange(sendjobid+4, 0);
				//System.out.print("send all data over!!! use time"+(Clock.currentTime()-senddatabeg));
			}
			else
			{
				reallysendbr+=sendthisbr;
				int mm=reallysendbr*100/sendall;
                                  mainframe.processbarchange(sendjobid+4, mm);
                             // JOptionPane.showInputDialog("recv up" );
                             //   mainframe.processbarchange(sendjobid, mm);
			//	JOptionPane.showInputDialog(mm+" "+reallysend+"  "+sendall);
			      
				sendnextbr();
			}
			
		 
	}
        
        
        
	public void dodatarecv(Message msg)
	{
		//this.send(null, "DATA  srcaddr:"+id+" desaddr:"+addr+" len:"+323+" ptkid:" +pktid+" "+" desmac:"+ desmac+" ");
		 String[] sArray=msg.toStringPR().split(":") ;
			String[] stmp1=sArray[1].split(" ");
			long srcaddr=Long.parseLong(stmp1[0]);
			String[] stmp2=sArray[2].split(" ");
			long desaddr=Long.parseLong(stmp2[0]);
		 
			String[] stmp3=sArray[3].split(" ");
			int desid=Integer.parseInt(stmp3[0]);
			String[] stmp4=sArray[4].split(" ");
			int pktid=Integer.parseInt(stmp4[0]);
			String[] stmp5=sArray[5].split(" ");
			int desmac=Integer.parseInt(stmp5[0]);
			String[] stmp6=sArray[6].split(" ");
			int srcid=Integer.parseInt(stmp6[0]);
			if(desmac!=mac)
				return;
			if(addr!=desaddr)
			{
				 
                            if(desaddr%addr==0)
                            {
                                desmac=finddes(desaddr);
                            }
                            else
                            {
                               desmac=fathermac;
                            }
				this.send(null, "DATARECV  srcaddr:"+srcaddr+" desaddr:"+desaddr+" desid:"+desid+" ptkid:" +pktid+" "+" desmac:"+ desmac+" srcid:"+srcid+" ");
			}
			else
			{
				if(desid!=id)
				{
					 JOptionPane.showInputDialog("bug desaddr match but id not match");
			 
					return;
				}

				recvup(srcid);
					//this.send(null, "DATARECV  srcaddr:"+desaddr+" desaddr:"+srcaddr+" desid:"+srcid+" ptkid:" +pktid+" "+" desmac:"+ fathermac+" srcid:"+desid+" ");
				 
			}
			
			 
			
	}
	
        
        public void doqdst(Message msg)
        {
              // this.send(null, "QDEST  srcaddr:"+addr+" desaddr:"+1+" desid:"+1+" desmac:"+ fathermac+" srcid:"+id+" qid:"+qid);
            	 String[] sArray=msg.toStringPR().split(":") ;
			String[] stmp1=sArray[1].split(" ");
			long srcaddr=Long.parseLong(stmp1[0]);
			String[] stmp2=sArray[2].split(" ");
			long desaddr=Long.parseLong(stmp2[0]);
		 
			String[] stmp3=sArray[3].split(" ");
			int desid=Integer.parseInt(stmp3[0]);
			String[] stmp4=sArray[4].split(" ");
			int desmac=Integer.parseInt(stmp4[0]);
			String[] stmp5=sArray[5].split(" ");
			int srcid=Integer.parseInt(stmp5[0]);
			String[] stmp6=sArray[6].split(" ");
			int qid=Integer.parseInt(stmp6[0]);
                        String[] stmp7=sArray[7].split(" ");
			long qaddr=Long.parseLong(stmp7[0]);
                         String[] stmp8=sArray[8].split(" ");
			int qlv=Integer.parseInt(stmp8[0]);
            if(desmac!=mac||worked==false)
            {
                return;
            }
            
            if(desmac==1)
            {
                 desmac=finddes(srcaddr);
                  qaddr=finddesaddrfromid(qid);
               //  mainframe.addtext(1, mac,desmac);
                prpkt=prpkt+mac+"->"+desmac+",";
                 prpktnum++;
                  if(prpktnum%10==0)
                                  {
                                  //   mainframe.settext(1, prpktnum); 
                                  }
                 this.send(null, "QDEST  srcaddr:"+1+" desaddr:"+srcaddr+" desid:"+1+" desmac:"+ desmac+" srcid:"+srcid+" qid:"+qid+" qaddr:"+qaddr+" qlevel:"+qlv+" ");
            }
            else if(srcaddr!=1)
            {
                //up
                //  mainframe.addtext(1, mac,fathermac);
                  prpkt=prpkt+mac+"->"+desmac+",";
                 prpktnum++;
                    if(prpktnum%10==0)
                                  {
                                  //   mainframe.settext(1, prpktnum); 
                                  }
                this.send(null, "QDEST  srcaddr:"+srcaddr+" desaddr:"+1+" desid:"+1+" desmac:"+ fathermac+" srcid:"+srcid+" qid:"+qid+" qaddr:"+qaddr+" qlevel:"+qlv+" ");
            }
            else
            {
                //down 
                if(srcid==id)
                {
                    if(qlv<qlevel)
                        return;
                   qlevel++;
                    clientdesaddr=qaddr;
                    needqaddr=false;
                      //JOptionPane.showInputDialog("find id"+ clientdesid+" addr "+clientdesaddr);
                    sendnext();
                }
                else if(desaddr==addr)
                {
                    //bug
                    
                }
                else
                {
                     desmac=finddes(desaddr);
                             //     mainframe.addtext(1, mac,desmac);
                       prpkt=prpkt+mac+"->"+desmac+",";
                 prpktnum++;
                   if(prpktnum%10==0)
                                  {
                                   //  mainframe.settext(1, prpktnum); 
                                  }
                 
                     this.send(null, "QDEST  srcaddr:"+srcaddr+" desaddr:"+desaddr+" desid:"+1+" desmac:"+ desmac+" srcid:"+srcid+" qid:"+qid+" qaddr:"+qaddr+" qlevel:"+qlv+" ");
                }
            }
         
        }
        
   
		 
		  
	
 
		 
		
	
 


	public void onMessage(Message msg) {
		
		
		 // System.out.print(msg.toStringPR());
		  String cmd=msg.toStringPR().substring(0, 5);
		  //System.out.print(cmd);
		  if(cmd.equals("HELLO"))
		  {
			 // System.out.print("recv hello");
			  doHello(msg);
		  }
		  else
		  {
			  super.addnuminclock();
			  if(cmd.equals("REQUE"))
			  {
				  doRequest(msg);
			  }
			  else if(cmd.equals("ROOTG"))
			  {
				  doRootgiveprim(msg);
			  }
			  else if(cmd.equals("ROOTT"))
			  {
				  doroottelllost(msg);
			  }
			  else if(cmd.equals("DATA "))
			  {
				  dodata(msg);
			  }
			  else if(cmd.equals("DATAR"))
			  {
				  dodatarecv(msg);
			  }
			  
			  else if(cmd.equals("PINGS"))
			  {
				  doping(msg);
			  }
			  else if(cmd.equals("PINGR"))
			  {
				  dopingreply(msg);
			  }
                            else if(cmd.equals("QDEST"))
			  {
				  doqdst(msg);
			  }
                        
                         
			  
		  }
		 
		  
	
 
		 
		
	}
}



/*
	 for(int i = 0;i < potfathervector.size();i++){
		 if(potfathervector.get(i).mac==potfmac)
		 {
			 potfathervector.remove(i);
			 potfathervector.add(0, new potFatherinfo(potfmac,potfid,1,potflevel));
			break;
		 }
		 else
		 {
			 if(potfathervector.get(i).level>potflevel)
			 {
				 potfathervector.add(i,new potFatherinfo(potfmac,potfid,1,potflevel))			 }
		   System.out.println(potfathervector.get(i));
		  }
   while(lit.hasNext())
   {
	   potFatherinfo tmp=lit.next();
	   if(tmp.mac==potfmac)
	   {
		  lit.remove();
		  lit.
	   }
	   if(tmp.level>potflevel)
	   {
		   
	   }
	   else
	   {
		   if(tmp.level==potflevel)
		   {
			   if()
		   }
		   else
		   {
		   }
	   }
	   
   }

	*/